/**
* @Description: WoShop商城
* @Author: 梧桐
* @Copyright: 武汉一一零七科技有限公司©版权所有
* @Link: www.s1107.com
* @Contact: QQ:2487937004
*/
<template>
	<view class="index_class">
		<view class="cont-order">
			<!-- <div class="tip flex" v-if="cartsureInfo.assem_zt.is_show == 1">
        <i class="iconfont">&#xe603;</i>
        <span>开团24小时内拼团成功，商家发货；若人数不足，系统自动退款关单；</span>
      </div> -->
			<view class="phone" @click="getnav('/pagesB/personalSon/address?isconfirm=true')">
				<i class="iconfont icon_e712"></i>
				<view class="tap_address" v-if="address">
					<view class="flex-align">
						{{address.contacts}}
						<text>{{address.phone}}</text>
					</view>
					<text>{{address.pro_name}}{{address.city_name}}{{address.area_name}} {{address.address}}</text>
				</view>
				<view class="tap_address" v-else>添加收货地址</view>
				<i class="iconfont icon_e60c phone-iconfont"></i>
				<image class="p_bottom" src="/static/images/img/address_img.png" />
			</view>
			<view v-for="(goodtime,keys) in goodinfo" :key="keys">
				<view class="fresh_list">
					<view class="fresh-name">
						<i class="iconfont">&#xe735;</i>
						{{goodtime.goodres[0].shop_name}}
					</view>
					<view v-for="(time,index) in goodtime.goodres" :key="index">
						<image class="fresh_img" :src="time.thumb_url" />
						<view class="fresh_banner">
							<!-- <view class="fresh_tip">好多绿蔬菜</view> -->
							<view class="fresh_text">{{time.goods_name}}</view>
							<text class="attr_color">{{time.goods_attr_str}}</text>
							<view class="address">
								<view class="pirve">
									¥{{time.shop_price}}
									<text>/1件</text>
								</view>
								<view>
									{{ time.shop_price | currency}}<text>/1件</text>
								</view>

							</view>
							<view class="shop">购买数量:{{time.goods_num}}</view>
						</view>
					</view>
				</view>

				<view class="adds_list">
					<view @click="coupon(1,goodtime)">
						<text>优惠券</text>
						<view>
							¥ {{goodtime.youhui_price}} <span>{{ goodtime.youhui_price | currency}}</span>
							<i class="iconfont icon_e60c"></i>
						</view>
					</view>

					<view @click="coupon(2,goodtime)">
						<text>运费</text>
						<view>
							¥ {{goodtime.freight}} {{ goodtime.freight | currency}} {{goodtime.freight ==0 ? '包邮': ''}}
							<i class="iconfont icon_e60c"></i>
						</view>
					</view>
					<view>
						<text>总计</text>
						<view>
							¥ {{goodtime.xiaoji_price}} {{ goodtime.xiaoji_price| currency}}
							<i class="iconfont icon_e60c" style="color:transparent!important"></i>
						</view>
					</view>
				</view>
			</view>
			<u-popup ref="popupCopun" mode="bottom" border-radius="12">
				<view class="popup-coupon" v-if="pop == 1">
					<view class="popup-title">优惠券</view>
					<view class="coupon-list">
						<view class="cuxiao">
							<text class="title">优惠券</text>
							<view class="content">
								<!-- <view style="font-size:22upx">safgasfsd</view> -->
								<view style="color:#999;font-size:22upx">
									{{infoCoupon.coupon_str? infoCoupon.coupon_str:'暂无优惠信息'}}</view>
							</view>
							<i class="iconfont"></i>
						</view>
						<view class="cuxiao" v-if="infoCoupon.cxhuodong?infoCoupon.cxhuodong.length>0:0">
							<text class="title">促销</text>
							<view class="content">
								<view style="color:#999;font-size:22upx;margin-bottom:20upx;"
									v-for="(cou,index9) in infoCoupon.cxhuodong" :key="index9">{{cou}}</view>
							</view>
							<i class="iconfont"></i>
						</view>
					</view>

					<view class="popup-com">
						<button @click="hidePopup">完成</button>
					</view>
				</view>

				<view class="popup-coupon" v-if="pop == 2">
					<view class="popup-title">配送方式</view>
					<view class="coupon-list">
						<view class="cuxiao">
							<text class="title"> 配送</text>
							<view class="content">
								<!-- <view style="font-size:22upx">safgasfsd</view> -->
								<view style="color:#999;font-size:22upx">{{infoCoupon.freight_str}}</view>
							</view>
							<i class="iconfont"></i>
						</view>
					</view>

					<view class="popup-com">
						<button @click="hidePopup">完成</button>
					</view>
				</view>
			</u-popup>
			<view class="amountFooter">
				<p class="left">
					<text>￥</text>
					<text class="price">{{cartsureInfo.zsprice}}</text>
				</p>
				<p>{{ cartsureInfo.zsprice | currency}}</p>
				<u-button hair-line ripple :custom-style="customStyle" style="width:240upx" shape="circle"
					@click="submitOrder">提交订单</u-button>

			</view>
		</view>

		<pay-pop ref="payPop" :callbackUrl="'/pagesC/homeSon/allOrder?index=6'"
			:totalPrice="Number(cartsureInfo.zsprice).toFixed(2)">
			<div class="pay-btn" @click="$u.debounce(crosspay,1500)">确认支付</div>
		</pay-pop>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	let cart_idresList = ""; // 购物车商品id
	let paydataYue = ""; // 提交商品订单
	let orderInfoData = {};
	import payPop from "../../components/payPop";
    
	export default {
		data() {
			return {
				infoCoupon: {},
				cartsureInfo: {},
				address: "", // 收货地址
				goodinfo: {},
				pop: 1,
				valShow: false,
				valueStatus: 0, // 更新页面状态 防止页面卡死
				params: null,
				wechatMiniProgram: 0,
				totalPrice: "0.00",
				customStyle: {
					'backgroundImage': 'linear-gradient(to right, #ffa468, #fa3f3f)',
					'color': '#fff',
					'margin': '0',
					'fontSize': '24upx'
				}
			};
		},
		components: {
			payPop
		},
		onShow() {
			paydataYue = "";
			if (this.$store.state.confirmAddress) {
				this.address = this.$store.state.confirmAddress;
			}
		},
		onLoad(opiton) {
			//#ifdef MP-WEIXIN
			this.wechatMiniProgram = 1;
			//#endif
			this.params = opiton;
			if (opiton.type == 1) {
				// 商品详情立即购买
				this.getpursure(opiton);
			} else if (opiton.type == 2) {
				// 购物车购买
				this.getcartsure(opiton);
			}
			cart_idresList = opiton;


			uni.setNavigationBarTitle({
				title: '立即结算'
			});

		},
		watch: {
			valShow() {
				/**
				 * 更新页面状态 防止页面卡死
				 */
				this.statusMath(this);
			}
		},
		onBackPress() {
			//
			if (this.valShow) {
				this.getnav("/pagesC/homeSon/allOrder?backindex=2");
				return true;
			}
		},
		methods: {
			coupon(type, data) {
				if (type == 1) {
					// 优惠
					if (Number(data.youhui_price) <= 0) {}
				} else if (type == 2) {
					// 运费
					if (Number(data.freight) <= 0) {}
				}

				this.pop = type;
				this.infoCoupon = data || {};
				this.$refs.popupCopun.open();
				console.log(infoCoupon);
			},
			/**
			 *  // 购物车购买
			 */
			getcartsure(data) {
				this.$http.getcartsure({
					cart_idres: data.cart_idres
				}).then(res => {
					if (res.status == 200) {
						this.cartsureInfo = res.data;
						this.address = res.data.address;
						this.goodinfo = res.data.goodinfo;
					}
				});
			},
			/**
			 * // 商品详情立即购买
			 */
			getpursure(data) {
				this.$http.getpursure({
						pur_id: data.pur_id,
						pin_number: data.pin_number,
						fangshi: data.fangshi
					})
					.then(res => {
						if (res.status == 200) {
							this.cartsureInfo = res.data || '';
							this.address = res.data.address || '';
							this.goodinfo = res.data.goodinfo || '';
						}
					});
			},
			/**
			 * 获取支付订单数据
			 *
			 */
			orderInfo(typeIndex) {
				let dataType = {
					dz_id: this.address.id,
					zf_type: typeIndex||2
				};
				return new Promise(reslove => {
					if (cart_idresList.type == 2) {

						// 购物车创建订单
						dataType.cart_idres = cart_idresList.cart_idres;
						this.$http.getaddorderPay(dataType).then(res => {
							if (res.status == 200) {
								reslove(res);
								this.$refs.payPop.payModel = true; // 弹出
							} else {
								this.$toast("订单提交失败");
							}
						});
					} else if (cart_idresList.type == 1) {
						// 立即购买创建订单
						dataType.pur_id = cart_idresList.pur_id;
						this.$http.getpuraddorderPay({
								...dataType,
								fangshi: this.params.fangshi,
								pin_number: this.params.pin_number
							})
							.then(res => {
								if (res.status == 200) {
									reslove(res);
									uni.showToast({
										title:'订单创建成功'
									})
									setTimeout(()=>{
										this.$refs.payPop.payModel = true; // 弹出
									},1500)
								} else {
									// this.$toast("订单提交失败");
									this.$toast(res.mess);
									this.$refs.uToast.show({
										title: res.mess,
										type: 'error',
										back: true
									})
								}
							});
					}
				});
			},
			// 生成拉起支付的密钥
			payFall(paydata,zf_type) {
				let data = {
					order_number: paydata.data.order_number,
					zf_type: zf_type,
					wechat_miniprogram: this.wechatMiniProgram
				}
				if (this.wechatMiniProgram === 1) {
					data.xcx_openid = uni.getStorageSync('xcxOpenid')
				}
				return new Promise(resolve => {
					this.$http.getzhifu(data)
						.then(res2 => {
							if (res2.status == 200) {
								resolve(res2);
							}
						});
				});
			},
			async submitOrder() {
				if (this.address == "") {
					return this.$toast('请先添加收货地址');
				}
				orderInfoData = await this.orderInfo();
				// this.$refs.payPop.payModel = true; // 弹出
			},

			async crosspay() {
				// this.$refs.payPop.payModel = false; // 弹出
				let ref = this.$refs.payPop
				// let paydata = await this.orderInfo(ref.payType);
				
				if (ref.payType === 5) {
					ref.orderNumber = orderInfoData.data.order_number
					ref.bankModel = true
					return
				} else {
					const temp = await this.payFall(orderInfoData,ref.payType);
					ref.payinfos = await temp.data.infos
					ref.crosspay()
				}


				this.$hideLoading()
			},

			banlancePay() {
				this.$http.getpdpaypwd().then(res => {
					if (res.status == 200) {
						if (res.data.zhifupwd == 0) {
							this.$toast('请先设置支付密码');
							setTimeout(() => {
								this.getnav("/pagesB/personalSon/setupZfpwd");
							}, 1000);
						} else {
							this.$refs.popupRef.close(); // 关闭
							this.valShow = true;
						}
					}
				});
			},



			hidePopup() {
				this.$refs.popupCopun.close(); // 关闭
			},

			/**
			 * 余额支付
			 */
			async codeShow(index) {
				// 接受子组件传的值  点击的六位密码值
				console.log(index);
				/**
				 * 确认支付
				 */
				this.$loading();
				this.$http
					.getzhifu({
						order_number: paydataYue.data.order_number,
						zf_type: paydataYue.data.zf_type,
						pay_password: index.cord
					})
					.then(res2 => {
						if (res2.status == 200) {
							this.$toast(res2.mess);
							setTimeout(() => {
								// this.valShow = index.backHied
								if (res2.data.leixing == 1) {
									this.getnav(
										"/pagesC/homeSon/orderDetails?order_num=" +
										res2.data.order_num +
										"&backindex=2"
									);
								} else if (res2.data.leixing == 2) {
									this.getnav(
										`/pagesC/homeSon/fightGroup?order_num=${res2.data.order_num}`
									);
								} else {
									this.getnav("/pagesC/homeSon/allOrder?index=2&backindex=2");
								}
							}, 1000);
						} else {
							this.$toast(res2.mess);
						}
					});
				console.log(paydataYue);
			},
			hideAl(bal) {
				this.valShow = false;
				this.getnav("/pagesC/homeSon/allOrder?backindex=2");
			}
		}
	};
</script>

<style lang="scss" scoped>
	.fresh-name {
		display: flex;
		align-items: center;
	}

	.money {
		padding-left: 20upx;
		font-size: 24upx;
		color: #999;
	}

	.icon_e712 {
		text-align: center;
		font-size: 45upx;
	}

	.cuxiao {
		margin-bottom: 40upx;
	}

	.tip {
		align-items: center;
		height: 98upx;
		padding: 0 30upx;
		color: #ed6a0c;
		background: #fffbe8;
		font-size: 26upx;

		i {
			padding-right: 10upx;
		}
	}

	.adds_list view {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.phone {
		display: flex;
		align-items: center;
		padding: 0 30upx;

		.tap_address {
			flex: 1;
			padding: 0 10upx;
		}

		.phone-iconfont {
			position: static !important;
			padding-right: 0;
			width: auto !important;
			margin: 0;
			padding: 0 !important;
		}
	}

	.amountFooter {
		display: flex;
		position: fixed;
		bottom: 0;
		width: 100%;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		padding: 12upx 30upx;

		.left {
			color: tomato;

			.price {
				font-size: 48upx;
				font-weight: bold;
			}
		}
	}

	.u-p-30 {
		padding: 30upx
	}

	.payWrap {
		.title {
			text-align: center;
		}

		.money {
			font-weight: bold;
			font-size: 52upx;
			color: red;
			text-align: center;
			margin: 40upx 0;
		}

		.item {
			padding: 20upx 0;

		}
	}
</style>
